Cells and windows
Cells and window objects can also emit events, and there is no need to clone event objects to assign multiple handlers (this is done automatically once the EventHandler
method is applied).
Cells
The following functions are available to extract cell objects (RemoteCellObj) from the evaluation context:
- EvaluationCell: the input cell (where the code is written)
- ResultCell: the output cell (even if it does not exist yet)
The following patterns are available to attach handlers:
- "Destroy"
- "State"
- "Error"
The most practical one is the first:
With[{},
EventHandler[ResultCell[], {"Destroy" -> (Print["Destroyed!"] &)}];
"Lovely day"
]
Try reevaluating it multiple times.
It can be used to unsubscribe from listeners or stop tasks once a cell has been reevaluated or destroyed.
Cell events also work for projected outputs to new windows.
Window
A WindowObj represents the current window where the notebook is running. It is usually used in FrontFetch and FrontSubmit, but it also supports a few handler patterns:
- "Closed"
Use the "Closed" event to remove tasks running in the background.
Example:
test = {0,1,0};
EventHandler[CurrentWindow[], {"Closed" -> Function[Null,
test = {1,0,0};
]}];
Graphics[{RGBColor[test // Offload], Disk[{0,0}, 1]}]
Try reloading the page or window and observe the changes.